package com.zsc.wxm.ysh.anjo.es.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.zsc.wxm.ysh.anjo.es.feign.OssService;
import com.zsc.wxm.ysh.anjo.es.feign.ResourcesService;
import com.zsc.wxm.ysh.anjo.es.service.SearchService;
import com.zsc.wxm.ysh.anjo.es.service.UploadService;
import com.zsc.wxm.ysh.anjo.result.Result;
import com.zsc.wxm.ysh.anjo.service.base.constant.EsConstant;
import com.zsc.wxm.ysh.anjo.service.base.model.es.vo.EsDataVO;
import com.zsc.wxm.ysh.anjo.utils.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述：
 *
 * @author playmaker
 * @since 2021/2/14 9:50
 */
@Service
@Slf4j
public class UploadServiceImpl implements UploadService {

    @Resource
    private OssService ossService;

    @Resource
    private RestHighLevelClient restHighLevelClient;

    @Resource
    private SearchService searchService;

    @Resource
    private ResourcesService resourcesService;

    @Override
    public void uploadHouseImage() {
        ossService.uploadHouseImage();
        log.info("------uploadHouseImage上传完成------");
    }

    @Override
    public boolean uploadHouseResourcesByCrawler(List<EsDataVO> list) throws IOException {
        if (CollUtil.isEmpty(list)) {
            log.info("导入es的数据为空");
            return false;
        }
        //先清除文档数据
        List<String> ids = list.stream().map(x -> x.getId().toString()).collect(Collectors.toList());
        searchService.deleteIndexDataByBatch(ids);
        //上传数据到es
        BulkRequest bulkRequest = new BulkRequest();
        for (EsDataVO vo : list) {
            //构造请求
            IndexRequest indexRequest = new IndexRequest(EsConstant.HOUSE_INDEX);
            //设置id
            indexRequest.id(vo.getId().toString());
            //设置数据
            indexRequest.source(JSON.toJSONString(vo), XContentType.JSON);
            bulkRequest.add(indexRequest);
        }
        BulkResponse responses = null;
        try {
            responses = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
            if (responses.hasFailures()) {
                log.error("批量索引出现错误：{}", responses.buildFailureMessage());
                BulkItemResponse[] items = responses.getItems();
                for (BulkItemResponse item : items) {
                    log.error("出错数据：{}", item);
                }
            }
        } catch (IOException e) {
            log.error(ExceptionUtil.getMessage(e));
            return false;
        }
        return true;
    }

    @Override
    public void createOrUpdateData(Long houseId) {
        Result result = resourcesService.getEsDataVoById(houseId);
        String jsonData = JSON.toJSONString(result.getData().get("esDataVo"));
        EsDataVO esDataVO = JSON.parseObject(jsonData, EsDataVO.class);
        //TODO
    }

    @Override
    public void removeData(Long houseId) throws IOException {
        searchService.deleteIndexDataByBatch(Collections.singletonList(houseId.toString()));
    }
}
